package com.autumn.platform.dao.dialect.impl;

import org.springframework.data.domain.Pageable;

import com.autumn.platform.dao.dialect.IDialect;

public abstract class DB2 extends AbstractDialect {

    private static final IDialect instance = new DB2() {};//唯一实例

    /**
     * 构造函数私有化
     */
    private DB2() {}

    /**
     * 获取唯一实例
     */
    public static IDialect getInstance() {
        return instance;
    }

    @Override
    public String getDatabaseId() {
        return "db2";
    }

    @Override
    protected String getPageSql(String sql, Pageable pageable) {
        StringBuilder sb = new StringBuilder();
        sb.append("select * from ( select row_.*, row_number() over() rownum_ from ( ");
        sb.append(sql);
        sb.append(" ) row_ ");
        sb.append(super.getSortSql(pageable.getSort()));
        sb.append(")  a where rownum_ > ").append(pageable.getOffset()).append(" and rownum_ <= ").append(pageable.getOffset() + pageable.getPageSize());
        return sb.toString();
    }
}
